/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormCadastroTarefa.java
 *
 * Created on 10/03/2012, 12:40:42
 */
package br.com.ufc.view;

import br.com.ufc.contoller.AgendaDataBase;
import br.com.ufc.contoller.Agendavel;
import br.com.ufc.model.TarefaDataBase;
import javax.swing.JOptionPane;

/**
 *
 * @author Paulo
 */
public class FormCadastroTarefa extends javax.swing.JDialog {

    /** Creates new form FormCadastroTarefa */
    public FormCadastroTarefa() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jpA = new javax.swing.JPanel();
        jlCodigo = new javax.swing.JLabel();
        jtfCodigo = new javax.swing.JTextField();
        jlDescricao = new javax.swing.JLabel();
        jtfDescricao = new javax.swing.JTextField();
        jpB = new javax.swing.JPanel();
        jlData = new javax.swing.JLabel();
        jtfData = new javax.swing.JTextField();
        jlHoraInicio = new javax.swing.JLabel();
        jtfHoraInicio = new javax.swing.JTextField();
        jlDuracao = new javax.swing.JLabel();
        jtfDuracao = new javax.swing.JTextField();
        jlValorHora = new javax.swing.JLabel();
        jtfValorHora = new javax.swing.JTextField();
        jpC = new javax.swing.JPanel();
        jlLocal = new javax.swing.JLabel();
        jtfLocal = new javax.swing.JTextField();
        jlImportancia = new javax.swing.JLabel();
        jtfImportancia = new javax.swing.JTextField();
        jlUrgencia = new javax.swing.JLabel();
        jtfUrgencia = new javax.swing.JTextField();
        jpD = new javax.swing.JPanel();
        jbSalvar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastro de Tarefa");
        setResizable(false);

        jlCodigo.setText("Código:");

        jtfCodigo.setEditable(false);

        jlDescricao.setText("Descrição:");

        javax.swing.GroupLayout jpALayout = new javax.swing.GroupLayout(jpA);
        jpA.setLayout(jpALayout);
        jpALayout.setHorizontalGroup(
            jpALayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpALayout.createSequentialGroup()
                .addGroup(jpALayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpALayout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jtfCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(jpALayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jlCodigo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(jpALayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlDescricao)
                    .addComponent(jtfDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, 287, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jpALayout.setVerticalGroup(
            jpALayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpALayout.createSequentialGroup()
                .addGroup(jpALayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlDescricao)
                    .addComponent(jlCodigo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpALayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtfCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jpB.setPreferredSize(new java.awt.Dimension(384, 51));
        jpB.setRequestFocusEnabled(false);

        jlData.setText("Data:");

        jlHoraInicio.setText("Hora de início:");

        jlDuracao.setText("Duração:");

        jlValorHora.setText("Valor da Hora:");

        javax.swing.GroupLayout jpBLayout = new javax.swing.GroupLayout(jpB);
        jpB.setLayout(jpBLayout);
        jpBLayout.setHorizontalGroup(
            jpBLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpBLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpBLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtfData, javax.swing.GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE)
                    .addComponent(jlData))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpBLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtfHoraInicio, javax.swing.GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE)
                    .addComponent(jlHoraInicio))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpBLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlDuracao)
                    .addComponent(jtfDuracao, javax.swing.GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpBLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlValorHora)
                    .addComponent(jtfValorHora, javax.swing.GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE))
                .addGap(12, 12, 12))
        );
        jpBLayout.setVerticalGroup(
            jpBLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpBLayout.createSequentialGroup()
                .addGroup(jpBLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpBLayout.createSequentialGroup()
                        .addComponent(jlData)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtfData, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jpBLayout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jtfHoraInicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jpBLayout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jtfDuracao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jpBLayout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jtfValorHora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jlHoraInicio)
                    .addComponent(jlDuracao)
                    .addComponent(jlValorHora))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jpC.setPreferredSize(new java.awt.Dimension(384, 51));

        jlLocal.setText("Local:");

        jlImportancia.setText("Importância");

        jlUrgencia.setText("Urgência");

        javax.swing.GroupLayout jpCLayout = new javax.swing.GroupLayout(jpC);
        jpC.setLayout(jpCLayout);
        jpCLayout.setHorizontalGroup(
            jpCLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpCLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpCLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlLocal)
                    .addComponent(jtfLocal, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpCLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jpCLayout.createSequentialGroup()
                        .addComponent(jlImportancia)
                        .addGap(40, 40, 40))
                    .addGroup(jpCLayout.createSequentialGroup()
                        .addComponent(jtfImportancia, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(jpCLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlUrgencia)
                    .addComponent(jtfUrgencia, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jpCLayout.setVerticalGroup(
            jpCLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpCLayout.createSequentialGroup()
                .addGroup(jpCLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpCLayout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addGroup(jpCLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jtfUrgencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jtfImportancia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jlUrgencia)
                    .addComponent(jlImportancia)
                    .addGroup(jpCLayout.createSequentialGroup()
                        .addComponent(jlLocal)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtfLocal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jpD.setPreferredSize(new java.awt.Dimension(384, 51));

        jbSalvar.setText("Salvar");
        jbSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbSalvarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jpDLayout = new javax.swing.GroupLayout(jpD);
        jpD.setLayout(jpDLayout);
        jpDLayout.setHorizontalGroup(
            jpDLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpDLayout.createSequentialGroup()
                .addContainerGap(304, Short.MAX_VALUE)
                .addComponent(jbSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jpDLayout.setVerticalGroup(
            jpDLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jbSalvar)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jpD, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 404, Short.MAX_VALUE)
                    .addComponent(jpA, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jpB, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 404, Short.MAX_VALUE)
                    .addComponent(jpC, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 404, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jpA, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(7, 7, 7)
                .addComponent(jpB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpD, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbSalvarActionPerformed
        /*
        AgendaDataBase bo = new AgendaDataBase();
        
        //cria uma nova tarefa a partir dos dados informados no formulario.
        TarefaDataBase tarefa = new TarefaDataBase();
        tarefa.setData(jtfData.getText());
        tarefa.setDescricao(jtfDescricao.getText());
        //tarefa.setDuracao(new Integer(jtfDuracao.getText()));
        tarefa.setHoraInicio(jtfHoraInicio.getText());
        //tarefa.setImportancia(new Integer(jtfImportancia.getText()));
        tarefa.setLocal(jtfLocal.getText());
        //tarefa.setUrgencia(new Integer(jtfUrgencia.getText()));
        //tarefa.setValorHora(new Double(jtfValorHora.getText()));
        try {
        if (bo.cadastrarTarefa(tarefa)) {
        JOptionPane.showMessageDialog(this, "Tarefa cadastrada com sucesso", "",
        JOptionPane.INFORMATION_MESSAGE);
        }
        } catch (Exception ex) {
        JOptionPane.showMessageDialog(this, ex.getMessage(), "Erro!",
        JOptionPane.ERROR_MESSAGE);
        }
         */

        SalvarTarefa();
    }//GEN-LAST:event_jbSalvarActionPerformed

    private Boolean validarPreenchimento() {
        if ((jtfDescricao.getText() == null) || (jtfDescricao.getText().equalsIgnoreCase(""))) {
            JOptionPane.showMessageDialog(this, "Informe a descrição da tarefa", "Descriçao é um campo obrigatório!",
                    JOptionPane.ERROR_MESSAGE);
            return false;
        }
        if ((jtfData.getText() == null) || (jtfData.getText().equalsIgnoreCase(""))) {
            JOptionPane.showMessageDialog(this, "Informe a data da tarefa", "Data é um campo obrigatório!",
                    JOptionPane.ERROR_MESSAGE);
            return false;
        }
        if ((jtfHoraInicio.getText() == null) || (jtfHoraInicio.getText().equalsIgnoreCase(""))) {
            JOptionPane.showMessageDialog(this, "Informe a hora de início da tarefa", "Hora de início é um campo obrigatório!",
                    JOptionPane.ERROR_MESSAGE);
            return false;
        }
        if ((jtfDuracao.getText() == null) || (jtfDuracao.getText().equalsIgnoreCase(""))) {
            JOptionPane.showMessageDialog(this, "Informe a duração da tarefa", "Duração é um campo obrigatório!",
                    JOptionPane.ERROR_MESSAGE);
            return false;
        }
        if ((jtfValorHora.getText() == null) || (jtfValorHora.getText().equalsIgnoreCase(""))) {
            JOptionPane.showMessageDialog(this, "Informe a valor da hora da tarefa", "Valor da hora é um campo obrigatório!",
                    JOptionPane.ERROR_MESSAGE);
            return false;
        }
        if ((jtfLocal.getText() == null) || (jtfLocal.getText().equalsIgnoreCase(""))) {
            JOptionPane.showMessageDialog(this, "Informe o local da tarefa", "Local é um campo obrigatório!",
                    JOptionPane.ERROR_MESSAGE);
            return false;
        }
        if ((jtfImportancia.getText() == null) || (jtfImportancia.getText().equalsIgnoreCase(""))) {
            JOptionPane.showMessageDialog(this, "Informe a importancia da tarefa", "Importancia é um campo obrigatório!",
                    JOptionPane.ERROR_MESSAGE);
            return false;
        }
        if ((jtfUrgencia.getText() == null) || (jtfUrgencia.getText().equalsIgnoreCase(""))) {
            JOptionPane.showMessageDialog(this, "Informe a urgência da tarefa", "Urgência de início é um campo obrigatório!",
                    JOptionPane.ERROR_MESSAGE);
            return false;
        }
        return true;
    }

    private void SalvarTarefa() {
        if (validarPreenchimento()) {

            //AgendaDataBase bo = new AgendaDataBase();
            Agendavel bo = FormAgendaEti.agenda;

            //cria uma nova tarefa a partir dos dados informados no formulario.
            TarefaDataBase tarefa = new TarefaDataBase();
            tarefa.setData(jtfData.getText());
            tarefa.setDescricao(jtfDescricao.getText());
            tarefa.setDuracao(new Integer(jtfDuracao.getText()));
            tarefa.setHoraInicio(jtfHoraInicio.getText());
            tarefa.setImportancia(new Integer(jtfImportancia.getText()));
            tarefa.setLocal(jtfLocal.getText());
            tarefa.setUrgencia(new Integer(jtfUrgencia.getText()));
            tarefa.setValorHora(new Double(jtfValorHora.getText()));
            try {
                if (bo.cadastrarTarefa(tarefa)) {
                    JOptionPane.showMessageDialog(this, "Tarefa cadastrada com sucesso", "Sucesso!",
                            JOptionPane.INFORMATION_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(this, "Array cheio!", "Aviso!",
                            JOptionPane.WARNING_MESSAGE);
                }
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, ex.getMessage(), "Erro!",
                        JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new FormCadastroTarefa().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jbSalvar;
    private javax.swing.JLabel jlCodigo;
    private javax.swing.JLabel jlData;
    private javax.swing.JLabel jlDescricao;
    private javax.swing.JLabel jlDuracao;
    private javax.swing.JLabel jlHoraInicio;
    private javax.swing.JLabel jlImportancia;
    private javax.swing.JLabel jlLocal;
    private javax.swing.JLabel jlUrgencia;
    private javax.swing.JLabel jlValorHora;
    private javax.swing.JPanel jpA;
    private javax.swing.JPanel jpB;
    private javax.swing.JPanel jpC;
    private javax.swing.JPanel jpD;
    private javax.swing.JTextField jtfCodigo;
    private javax.swing.JTextField jtfData;
    private javax.swing.JTextField jtfDescricao;
    private javax.swing.JTextField jtfDuracao;
    private javax.swing.JTextField jtfHoraInicio;
    private javax.swing.JTextField jtfImportancia;
    private javax.swing.JTextField jtfLocal;
    private javax.swing.JTextField jtfUrgencia;
    private javax.swing.JTextField jtfValorHora;
    // End of variables declaration//GEN-END:variables
}
